package com.tanhua.admin.controller;

import com.tanhua.admin.service.ManageService;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.domain.FreezeInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.SecureRandom;
import java.util.Date;
import java.util.Map;

@RequestMapping("/manage")
@RestController
public class ManageController {
    @Autowired
    private ManageService manageService;

    //用户数据翻页
    @GetMapping("/users")
    public ResponseEntity users(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        //// TODO: 2022/2/17 在service查询时需要添加userState状态信息
        PageResult vo = manageService.users(page, pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 查询用户详情
     * GET /:userID （用户Id）
     * 返回值 userInfo
     */
    @GetMapping("/users/{userId}")
    public ResponseEntity findUserById(@PathVariable("userId") Long userId) {
        //// TODO: 2022/2/17 在service查询时需要添加userState状态信息
        UserInfo userInfo = manageService.findUserById(userId);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 查询视频列表
     * GET /manage/videos
     */
    @GetMapping("/videos")
    public ResponseEntity findVideos(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize,
                                     Long uid
    ) {
        PageResult vo = manageService.findVideos(page, pagesize, uid);
        return ResponseEntity.ok(vo);
    }

    //查询动态列表
    @GetMapping("/messages")
    public ResponseEntity findMovement(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       Long uid,
                                       String state) {
        Integer states = null;
        if (StringUtils.isNotEmpty(state) && !"''".equals(state)) {
            states = Integer.valueOf(state);
        }
        PageResult vo = manageService.findMovement(page, pagesize, uid, states);
        return ResponseEntity.ok(vo);
    }

    //GET/manage/messages/:id 消息id
    //根据id查询动态
    @GetMapping("/messages/{id}")
    public ResponseEntity findMovementVo(@PathVariable("id") String id) {
        MovementsVo vo = manageService.findMovementVo(id);
        return ResponseEntity.ok(vo);
    }

    //GET/manage/messages/comments
    //查询评论列表
    @GetMapping("/messages/comments")
    public ResponseEntity findComments(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       String messageID) {
        PageResult vo = manageService.findComments(messageID, page, pagesize);
        return ResponseEntity.ok(vo);
    }

    //POST/manage/users/freeze
    //用户冻结操作
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Map map) {
        //1. 获取请求参数
        Integer userId = (Integer) map.get("userId");   //--用户id

        String freezingTimeStr = (String) map.get("freezingTime");   //--冻结时间，1为冻结3天，2为冻结7天，3为永久冻结
        Integer freezingTime = Integer.valueOf(freezingTimeStr);

        String freezingRangeStr = (String) map.get("freezingRange"); //--冻结范围，1为冻结登录，2为冻结发言，3为冻结发布动态
        Integer freezingRange = Integer.valueOf(freezingRangeStr);

        String reasonsForFreezing = (String) map.get("reasonsForFreezing");   //--冻结原因
        String frozenRemarks = (String) map.get("frozenRemarks");     //--冻结备注
        FreezeInfo freezeInfo = new FreezeInfo();
        freezeInfo.setUserId(userId);
        freezeInfo.setFreezingTime(freezingTime);
        freezeInfo.setFreezingRange(freezingRange);
        freezeInfo.setFrozenRemarks(frozenRemarks);
        freezeInfo.setReasonsForFreezing(reasonsForFreezing);
        freezeInfo.setCreated(new Date());
        //2. 调用service中的方法
        manageService.save(freezeInfo);
        //3. 返回数据（message--String  操作结果--操作成功）
        String message = "操作成功";
        return ResponseEntity.ok(message);
    }

    //POST/manage/users/unfreeze
    //用户解冻操作
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map map){
        //1. 获取请求参数
        Integer userId = (Integer) map.get("userId");   //--用户id
        String reasonsForThawing = (String) map.get("reasonsForThawing");   //--解冻原因
        //2. 调用service方法
        manageService.unfreeze(userId,reasonsForThawing);
        //3. 返回结果
        return ResponseEntity.ok("操作成功");
    }
}
